package com.ruoyi.project.system.controller;

import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.ruoyi.common.utils.AlibabaApi;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SmExtract;
import com.ruoyi.project.system.mapper.SmExtractMapper;
import com.ruoyi.project.system.service.ISmExtractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 提现Controller
 */
@RestController
@RequestMapping("/system/extract")
public class SmExtractController extends BaseController {

    @Autowired
    private ISmExtractService smExtractService;

    @Autowired
    private SmExtractMapper smExtractMapper;

    @GetMapping("/wallet")
    public AjaxResult wallet() {
        Integer userId = SecurityUtils.getUserId().intValue();
        return success(smExtractMapper.extractList(userId));
    }

    /**
     * 管理系统发起转账
     */
    @GetMapping("/admCashOut/{extractId}")
    public AjaxResult admCashOut(@PathVariable("extractId") @NotBlank String extractId) {
        SmExtract sysExtract = smExtractService.selectSmExtractByExtractId(extractId);
        if ("转账中".equals(sysExtract.getStatus()) || "已转账".equals(sysExtract.getStatus())) {
            return error(sysExtract.getStatus());
        } else {
            try {
                AlipayFundTransToaccountTransferResponse transfer = AlibabaApi.transfer(sysExtract);
                System.out.println(transfer);
                if ("10000".equals(transfer.getCode())) {
                    smExtractService.updateSmExtract(new SmExtract(transfer));
                    return success(transfer.getMsg());
                } else {
                    smExtractService.updateSmExtract(new SmExtract(extractId, transfer.getSubMsg()));
                    return error(transfer.getSubMsg());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return error(e.getMessage());
            }
        }
    }

    /**
     * 刷新转账状态
     */
    @PostMapping("/flash")
    public AjaxResult flash(SmExtract sysExtract) {
        try {
            AlipayFundTransCommonQueryResponse transfer = AlibabaApi.query(sysExtract);
            if ("10000".equals(transfer.getCode()) && "SUCCESS".equals(transfer.getStatus())) {
                smExtractService.updateSmExtract(new SmExtract(transfer));
                return success(transfer.getMsg());
            } else {
                return success(transfer.getSubMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 查询提现列表
     */
    @PreAuthorize("@ss.hasPermi('system:extract:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmExtract smExtract) {
        startPage();
        List<SmExtract> list = smExtractService.selectSmExtractList(smExtract);
        return getDataTable(list);
    }

    /**
     * 导出提现列表
     */
    @PreAuthorize("@ss.hasPermi('system:extract:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmExtract smExtract) {
        List<SmExtract> list = smExtractService.selectSmExtractList(smExtract);
        ExcelUtil<SmExtract> util = new ExcelUtil<SmExtract>(SmExtract.class);
        util.exportExcel(response, list, "提现数据");
    }

    /**
     * 获取提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:extract:query')")
    @GetMapping(value = "/{extractId}")
    public AjaxResult getInfo(@PathVariable("extractId") String extractId) {
        return success(smExtractService.selectSmExtractByExtractId(extractId));
    }

    /**
     * 新增提现
     */
    @PreAuthorize("@ss.hasPermi('system:extract:add')")
    @PostMapping
    public AjaxResult add(@RequestBody SmExtract smExtract) {
        return toAjax(smExtractService.insertSmExtract(smExtract));
    }

    /**
     * 修改提现
     */
    @PreAuthorize("@ss.hasPermi('system:extract:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody SmExtract smExtract) {
        return toAjax(smExtractService.updateSmExtract(smExtract));
    }
}
